package com.soup.view;

import com.soup.dao.BookDao;
import com.soup.domain.Book;
import com.soup.utils.StringUtils;
import com.soup.view.component.Context;
import com.soup.view.component.MassageTip;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class BookFrame extends Context implements ActionListener {

    public JTextField name = new JTextField(10);
    public JTextField author = new JTextField(10);
    public JTextField publish = new JTextField(10);
    public JTextField have = new JTextField(10);

    private JButton add = new JButton("添加图书");

    private BookDao dao = new BookDao();

    public BookFrame(int width, int height) {
        super(width, height);

        name.setBounds(60,0,width-120,40);
        author.setBounds(60,50,width-120,40);
        publish.setBounds(60,100,width-120,40);
        have.setBounds(60,150,width-120,40);
        this.add(name, "名称", 100);
        this.add(author, "作者", 100);
        this.add(publish, "出版社", 100);
        this.add(have, "库存", 100);

        add.setBounds(60,300,width-120,40);
        this.add(add,20);
        add.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == add){
            if (!StringUtils.isEmpty(name.getText()) &&
                    !StringUtils.isEmpty(author.getText()) &&
                    !StringUtils.isEmpty(publish.getText()) &&
                    !StringUtils.isEmpty(have.getText())){

                Book book = new Book();
                book.setName(name.getText());
                book.setAuthor(author.getText());
                book.setPublish(publish.getText());
                book.setHot(0);
                book.setHave(Integer.parseInt(have.getText()));
                int result = dao.addBook(book);
                if (result > 0){
                    MassageTip.showMassage(null,"图书添加成功");
                }else{
                    MassageTip.showMassage(null,"图书添加失败");
                }
            }else{
                MassageTip.showMassage(null,"字段不可为空");
            }
        }
    }
}
